package com.gfilechooser.utils;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.net.Uri;
import android.webkit.MimeTypeMap;

public class MimeTypes {

	private Map<String, String> mimeTypes;

	public MimeTypes() {
		mimeTypes = new HashMap<String, String>();
	}

	@SuppressLint("DefaultLocale")
	public void put(String type, String extension) {
		extension = extension.toLowerCase();
		mimeTypes.put(type, extension);
	}

	@SuppressLint("DefaultLocale")
	public String getMimeType(String filename) {

		String extension = FileUtils.getExtension(filename);
		if (extension.length() > 0) {
			String webkitMimeType = MimeTypeMap.getSingleton()
					.getMimeTypeFromExtension(extension.substring(1));

			if (webkitMimeType != null) {
				return webkitMimeType;
			}
		}
		extension = extension.toLowerCase();

		String mimetype = mimeTypes.get(extension);

		if (mimetype == null)
			mimetype = "*/*";

		return mimetype;
	}

	public String getMimeType(Uri uri) {
		return getMimeType(FileUtils.getFile(uri).getName());
	}

}
